package com.leixin.utils;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Description: 日期处理公共类
 * @Author : LeiXin
 */
public class DateUtils {

    private static final String DATE_SIMPLE_FORMAT = "yyyyMMdd";
    private static final String TIME_SIMPLE_FORMAT = "HHmmss";
    private static final String DATETIME_SIMPLE_FORMAT = DATE_SIMPLE_FORMAT + TIME_SIMPLE_FORMAT;
    private static final String DATE_STANDARD_FORMAT = "yyyy-MM-dd";
    private static final String TIME_STANDARD_FORMAT = "HH:mm:ss";
    private static final String DATETIME_STANDARD_FORMAT = DATE_STANDARD_FORMAT + " " + TIME_STANDARD_FORMAT;

    public static LocalDateTime simpleStr2LocalDateTime(String dateStr) {
        return LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern(DATETIME_SIMPLE_FORMAT));
    }

    public static LocalDate simpleStr2LocalDate(String dateStr) {
        return LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(DATE_SIMPLE_FORMAT));
    }

    public static LocalTime simpleStr2LocalTime(String dateStr) {
        return LocalTime.parse(dateStr, DateTimeFormatter.ofPattern(TIME_SIMPLE_FORMAT));
    }

    public static LocalDateTime standardStr2LocalDateTime(String dateStr) {
        return LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern(DATETIME_STANDARD_FORMAT));
    }

    public static LocalDate standardStr2LocalDate(String dateStr) {
        return LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(DATE_STANDARD_FORMAT));
    }

    public static LocalTime standardStr2LocalTime(String dateStr) {
        return LocalTime.parse(dateStr, DateTimeFormatter.ofPattern(TIME_STANDARD_FORMAT));
    }

    public static String nowSimpleDateTime() {
        return simpleDateTime(LocalDateTime.now());
    }

    public static String nowSimpleDate() {
        return simpleDate(LocalDate.now());
    }

    public static String nowSimpleTime() {
        return simpleTime(LocalTime.now());
    }

    public static String simpleDateTime(LocalDateTime dateTime) {
        return dateTime.format(DateTimeFormatter.ofPattern(DATETIME_SIMPLE_FORMAT));
    }

    public static String simpleDate(LocalDate date) {
        return date.format(DateTimeFormatter.ofPattern(DATE_SIMPLE_FORMAT));
    }

    public static String simpleTime(LocalTime time) {
        return time.format(DateTimeFormatter.ofPattern(TIME_SIMPLE_FORMAT));
    }


    public static String nowStandardDateTime() {
        return standardDateTime(LocalDateTime.now());
    }

    public static String nowStandardDate() {
        return standardDate(LocalDate.now());
    }

    public static String nowStandardTime() {
        return standardTime(LocalTime.now());
    }

    public static String standardDateTime(LocalDateTime dateTime) {
        return dateTime.format(DateTimeFormatter.ofPattern(DATETIME_STANDARD_FORMAT));
    }

    public static String standardDate(LocalDate date) {
        return date.format(DateTimeFormatter.ofPattern(DATE_STANDARD_FORMAT));
    }

    public static String standardTime(LocalTime time) {
        return time.format(DateTimeFormatter.ofPattern(TIME_STANDARD_FORMAT));
    }

    /**
     * =================================== 日期转换 =================================
     **/
    private static ZoneId zoneId = ZoneId.systemDefault();

    public static Date toDate(LocalDateTime localDateTime) {
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        return Date.from(zdt.toInstant());
    }

    public static Date toDate(LocalDate localDate) {
        ZonedDateTime zdt = localDate.atStartOfDay().atZone(zoneId);
        return Date.from(zdt.toInstant());
    }

    public static Date toDate(LocalTime localTime) {
        LocalDate localDate = LocalDate.of(1970, 1, 1);
        LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        return Date.from(zdt.toInstant());
    }

    public static LocalDateTime toLocalDateTime(Date date) {
        Instant instant = date.toInstant();
        return instant.atZone(zoneId).toLocalDateTime();
    }

    public static LocalDate toLocalDate(Date date) {
        Instant instant = date.toInstant();
        return instant.atZone(zoneId).toLocalDate();
    }

    public static LocalTime toLocalTime(Date date) {
        Instant instant = date.toInstant();
        return instant.atZone(zoneId).toLocalTime();
    }

    public static String dateToDateTimeStr(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    public static String dateToDateStr(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }

    public static String dateToTimeStr(Date date) {
        return new SimpleDateFormat("HH:mm:ss").format(date);
    }

    public static String localDateTimeToStr(LocalDateTime dateTime) {
        return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(dateTime);
    }

    public static String localDateToStr(LocalDate date) {
        return DateTimeFormatter.ofPattern("yyyy-MM-dd").format(date);
    }

    public static String localTimeToStr(LocalTime time) {
        return DateTimeFormatter.ofPattern("HH:mm:ss").format(time);
    }

    public static LocalDateTime timestamToDatetime(long timestamp){
        Instant instant = Instant.ofEpochMilli(timestamp);
        return LocalDateTime.ofInstant(instant, zoneId);
    }

    public static long toTimestamp(LocalDateTime ldt){
        Instant instant = ldt.atZone(zoneId).toInstant();
        return instant.toEpochMilli();
    }

    public static long toTimestampSecond(LocalDateTime ldt){
        Instant instant = ldt.atZone(zoneId).toInstant();
        return instant.toEpochMilli() / 1000;
    }

    public static long nowTimestampSecond() {
        return new Date().getTime() / 1000;
    }

    public static long nowTimestampMilli() {
        return new Date().getTime();
    }



}
